//---------------------------------------------------------------------
// Copyright (C) 2010 - Cobalt Solution / Sebastien Leon              |
//---------------------------------------------------------------------
// This software is under dual licence: commercial licence + GPL v3   |
// To keep closed your modification of this software, you need to     |
// contact me to obtain a commercial licence.                         |
// Any other derivated work should remain Open Source.                |
// Contact me for a licence, a question or if you want to commit      |
// your code to the main tree: seb at cobaltsolutions dot info        |
//---------------------------------------------------------------------

#ifndef _LOOMVIEW_H_
#define _LOOMVIEW_H_

#include <QGraphicsView>
#include <QGraphicsRectItem>
#include <QPropertyAnimation>

class Loom;
class MainForm;
class DropFeedBack;

class AllLooms : public QGraphicsItem
{
public:
  AllLooms():QGraphicsItem(NULL),m_on(false){}
  void   Dragging(bool on) { if (on != m_on) { m_on=on; update(); }}
  bool   IsOn () const { return m_on; }
  QRectF boundingRect() const { return QRectF(QPointF(), QSize(110,35) );}
  void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  {
    Q_UNUSED(option); Q_UNUSED(widget);
    painter->setPen  (QPen(Qt::black,1));
    painter->setBrush(m_on ? Qt::red : Qt::gray);
    painter->drawRect(boundingRect());
    painter->drawText(boundingRect(), Qt::AlignCenter | Qt::TextSingleLine, "Envoyer a tous");
  }
protected:
  bool m_on;
};
//----------------------------------------------------
//----------------------------------------------------
class LoomView : public QGraphicsView
{
  Q_OBJECT

  enum EAction { e_none, e_move, e_resize };
public:
  LoomView      ( QWidget * parent );
  void SetForm  ( MainForm& main   );
protected:
  MainForm*     mp_mainForm;
  DropFeedBack* m_dropFeedBack;
  QPointF       m_startPos;
  QPointF       m_startValue;
  Loom*         m_startLoom;
  EAction       m_action;
  bool CanEditItems();
  virtual void dragEnterEvent ( QDragEnterEvent* event );
  virtual void dragLeaveEvent ( QDragLeaveEvent* event );
  virtual void dragMoveEvent  ( QDragMoveEvent*  event );
  virtual void dropEvent      ( QDropEvent*      event );

  virtual void mousePressEvent      (QMouseEvent *event);
  virtual void mouseMoveEvent       (QMouseEvent *event);
  virtual void mouseReleaseEvent    (QMouseEvent *event);
  virtual void mouseDoubleClickEvent(QMouseEvent *event);

  virtual void resizeEvent          (QResizeEvent*event);
//  virtual void drawBackground   (QPainter* painter, const QRectF & rect );
//  virtual void drawForeground   (QPainter* painter, const QRectF & rect );

  AllLooms m_allLooms;
};
//----------------------------------------------------
//----------------------------------------------------
class DropFeedBack : public QObject, public QGraphicsRectItem
{
  Q_OBJECT
  Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity)
public:
  DropFeedBack     (LoomView& lview);
  bool  UpdateLoom (const QPoint& pos);
  Loom* LoomTarget () { return mp_l; } 
protected:
  QPropertyAnimation m_anim;
  LoomView& mr_lview;
  Loom*     mp_l;
};

#endif // _LOOMVIEW_H_